# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file '子窗口的动态添加及排列.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2 import QtWidgets, QtGui, QtCore
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")

        # 创建MDI窗口区域
        self.mdiArea = QMdiArea(self.centralwidget)
        self.mdiArea.setObjectName(u"mdiArea")
        self.mdiArea.setGeometry(QRect(0, 0, 800, 800))

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 26))
        # 设置主菜单
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        self.menu.setTitle("子窗口操作")
        MainWindow.setMenuBar(self.menubar)

        # 设置新建菜单项
        self.actionxinjian = QtWidgets.QAction(MainWindow)
        self.actionxinjian.setObjectName("actionxinjian")
        self.actionxinjian.setText("新建")

        # 设置平铺菜单项
        self.actionpingpu = QtWidgets.QAction(MainWindow)
        self.actionpingpu.setObjectName("actionpingpu")
        self.actionpingpu.setText("平铺显示")

        # 设置级联菜单项
        self.actionjilian = QtWidgets.QAction(MainWindow)
        self.actionjilian.setObjectName("actionjilian")
        self.actionjilian.setText("级联显示")

        # 将新建的三个菜单项添加到主菜单中
        self.menu.addAction(self.actionxinjian)
        self.menu.addAction(self.actionpingpu)
        self.menu.addAction(self.actionjilian)
        # 将设置完的主菜单添加到菜单栏中
        self.menubar.addAction(self.menu.menuAction())

        # 为菜单栏关联信号
        self.menubar.triggered[QAction].connect(self.action)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi
    count = 0

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u5b50\u7a97\u53e3\u7684\u52a8\u6001\u6dfb\u52a0\u53ca\u6392\u5217", None))
    # retranslateUi

    # 自定义槽函数，根据选择的菜单执行相应的操作
    def action(self, m):
        if m.text() == '新建':
            sub = QMdiSubWindow()
            self.count = self.count + 1         # 记录子窗口个数
            # 设置子窗口标题
            sub.setWindowTitle("子窗口" + str(self.count))
            # 在子窗口中添加一个标签，并设置文本
            sub.setWidget(QLabel("这是第 %d 个子窗口" % self.count))
            self.mdiArea.addSubWindow(sub)
            sub.show()
        elif m.text() == '平铺显示':
            self.mdiArea.tileSubWindows()
        elif m.text() == "级联显示":
            self.mdiArea.cascadeSubWindows()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

